package com.lovemanai.test;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.lovemanai.entity.City;
import com.lovemanai.entity.StuInfo;
import com.lovemanai.entity.TeacherInfo;
import com.lovemanai.entity.User;
import com.lovemanai.util.HibernateSessionFactory;

public class Demo1 {
	
	/**
	 * 测试Many-to-One查询
	 */
	@Test
	public void test1() {
		Configuration conf = new Configuration().configure();
		SessionFactory sf = conf.buildSessionFactory();
		Session s = sf.openSession();
		Transaction tx = s.beginTransaction();
//		Session s = HibernateSessionFactory.getSession();
		User user = (User) s.get(User.class, 4);
		System.out.println(user.getCity().getName());
		tx.commit();
		s.close();
	}
	
	
	@Test
	public void test2() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		
		City city = (City) s.get(City.class, 1);//只有持久化的对象才能写入数据库
		User user = new User();
		user.setAge(23);
		user.setCity(city);
		user.setUname("王五");
		s.save(user);
		tx.commit();
	}
	
	@Test
	public void test3() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		
		City city = (City) s.get(City.class, 2);
		User user = (User) s.get(User.class, 6);
		
		user.setCity(city);
		s.update(user);
		
		tx.commit();
	}
	
	@Test
	public void test4() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		City city = (City) s.get(City.class, 2);
		Set<User> set = city.getUsers();
		for (User user : set) {
			System.out.println(user.getUname());
		}
		tx.commit();
		s.close();
	}
	
	@Test
	public void test5() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		TeacherInfo t = (TeacherInfo) s.get(TeacherInfo.class, 1);
		Set<StuInfo> students = t.getStudents();
		for (StuInfo stuInfo : students) {
			System.out.println(stuInfo.getName());
		}
		tx.commit();
		s.close();
	}
	
	/**
	 * 级联添加
	 * 添加User级联添加City
	 * 在User.hbm.xml中many-one属性添加属性cascade="all"或者cascade="sava-update"
	 */
	@Test
	public void test6() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		City city = new City("合肥");
		User user = new User("赵四", 15, city);
		s.save(user);
		tx.commit();
		s.close();
	}
	
	/**
	 * 级联删除
	 * 删除City时级联删除User
	 * 在City.hbm.xml中添加属性cascade="delete"
	 */
	@Test
	public void test7() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		City city = (City) s.get(City.class, 3);
		s.delete(city);
		tx.commit();
		s.close();
	}
	
	/**
	 * 级联添加，主控方为many
	 */
	@Test
	public void test8() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		City city = new City("南京");
		User user = new User("黄月英", 18, city);
		s.save(user);
		tx.commit();
		s.close();
	}
	
	/**
	 * 
	 */
	@Test
	public void test9() {
		Session s = HibernateSessionFactory.getSession();
		Transaction tx = s.beginTransaction();
		City city = new City("京都");
		User user = new User();
		user.setUname("花泽香菜");
		user.setAge(20);
		user.setCity(city);//设置关系
		city.getUsers().add(user);//设置关系
		s.save(city);
		tx.commit();
		s.close();
	}
	
	/**
	 * 级联操作中间表
	 * 主控方为Teacher，Student反转
	 */
	@Test
	public void test10() {
		Session s = HibernateSessionFactory.getSession();
		TeacherInfo t = (TeacherInfo) s.get(TeacherInfo.class, 1);
		StuInfo s1 = (StuInfo) s.get(StuInfo.class, 1);
		StuInfo s2 = (StuInfo) s.get(StuInfo.class, 2);
		t.getStudents().add(s1);
		t.getStudents().add(s2);
		Transaction tx = s.beginTransaction();
		s.update(t);
		tx.commit();
		s.close();
	}
	
	/**
	 * 中间表级联修改
	 * 1.取出两个老师对象
	 * 2.取出学生对象
	 * 3.从老师1中移除学生
	 * 4.将该学生添加到老师2中
	 * 5.更新老师1，老师2
	 */
	@Test
	public void test11() {
		Session s = HibernateSessionFactory.getSession();
		TeacherInfo t1 = (TeacherInfo) s.get(TeacherInfo.class, 1);
		StuInfo s2 = (StuInfo) s.get(StuInfo.class, 2);
		t1.getStudents().remove(s2);
		TeacherInfo t2 = (TeacherInfo) s.get(TeacherInfo.class, 2);
		t2.getStudents().add(s2);
		Transaction tx = s.beginTransaction();
		s.update(t1);
		s.update(t2);
		tx.commit();
		s.close();
	}
	
	/**
	 * 中间表级联删除
	 */
	@Test
	public void test12() {
		Session s = HibernateSessionFactory.getSession();
		TeacherInfo t1 = (TeacherInfo) s.get(TeacherInfo.class, 1);
		StuInfo s1 = (StuInfo) s.get(StuInfo.class, 1);
		t1.getStudents().remove(s1);//从t1中移除s1
		Transaction tx = s.beginTransaction();
		s.update(t1);//更新
		tx.commit();
		s.close();
	}
}
